<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Array Cardio 1</title>
	</head>
	<body>
		<h1>打开控制台查看输出结果</h1>
	</body>

	<script type="text/javascript">
		const inventors = [
			{
        first: 'Albert',
        last: 'Einstein',
        year: 1879,
        passed: 1955
      },
      {
        first: 'Isaac',
        last: 'Newton',
        year: 1643,
        passed: 1727
      },
      {
        first: 'Galileo',
        last: 'Galilei',
        year: 1564,
        passed: 1642
      },
      {
        first: 'Marie',
        last: 'Curie',
        year: 1867,
        passed: 1934
      },
      {
        first: 'Johannes',
        last: 'Kepler',
        year: 1571,
        passed: 1630
      },
      {
        first: 'Nicolaus',
        last: 'Copernicus',
        year: 1473,
        passed: 1543
      },
      {
        first: 'Max',
        last: 'Planck',
        year: 1858,
        passed: 1947
      },
      {
        first: 'Katherine',
        last: 'Blodgett',
        year: 1898,
        passed: 1979
      },
      {
        first: 'Ada',
        last: 'Lovelace',
        year: 1815,
        passed: 1852
      },
      {
        first: 'Sarah E.',
        last: 'Goode',
        year: 1855,
        passed: 1905
      },
      {
        first: 'Lise',
        last: 'Meitner',
        year: 1878,
        passed: 1968
      },
      {
        first: 'Hanna',
        last: 'Hammarström',
        year: 1829,
        passed: 1909
      }
    ];

    const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry',
      'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert',
      'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester',
      'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano',
      'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle',
      'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose',
      'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert',
      'Blair, Tony', 'Blake, William'
    ];

    const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car',
      'truck', 'pogostick'
    ];

		console.clear();

		//1.筛选 16 世纪出生的发明家
		const __fifteen = inventors.filter( inventor => (inventor.year >= 1500 && inventor.year < 1600));
		console.log('1.筛选 16 世纪出生的发明家');
		console.table(__fifteen);

		//2.展示他们的姓和名
		const fullNames = inventors.map( inventor => `${inventor.first} ${inventor.last}`);
		console.log('2.展示他们的姓和名');
		console.log(fullNames);

		//3.把他们按照年龄从大到小进行排序
		const ordered = inventors.sort((a, b) => {
			return a.year > b.year ? 1 : -1;
		});
		console.log('3.把他们按照年龄从大到小进行排序');
		console.table(ordered);

		//4.计算所有的发明家加起来一共活了多少岁
		const totalYears = inventors.reduce((total, inventor) => {
			return total + (inventor.passed - inventor.year);
		}, 0)
		console.log('4.计算所有的发明家加起来一共活了多少岁');
		console.log(totalYears);

		//5.按照他们活了多久来进行排序
		const oldest = inventors.sort((a, b) => {
			const lastInventor = a.passed - a.year;
			const nextInventor = b.passed - b.year;
			if (lastInventor > nextInventor) {
				return 1;
			} else {
				return -1;
			}
		});
		console.log('5.按照他们活了多久来进行排序');
		console.table(oldest);

		//6.map、filter结合使用筛选出网页中含有CSS标题的数据名称
		//https://book.douban.com/tag/web
    console.log('6.map、filter结合使用筛选出网页中含有CSS标题的数据名称');
    console.log(
    	`进入 https://book.douban.com/tag/web 打开控制台，执行以下操作:
    	const category = document.querySelectorAll('.subject-list h2 a');
		const links = Array.from(category);
		const CSS_BOOK = links
           .map(link => link.textContent)
           .filter(streetName => streetName.includes('CSS'));`
    );

		//7.按照姓氏来对发明家进行排序
		const alpha = people.sort((lastOne, nextOne) => {
			const [aLast, aFirst] = lastOne.split(', ');
			const [bLast, bFirst] = nextOne.split(', ');
			return aFirst > bFirst ? 1 : -1;
		});
		console.log('7.按照姓氏来对发明家进行排序');
		console.log(alpha);

		//8.统计给出数组中各个物品的数量
		const transportation = data.reduce((obj, item) => {
			if (!obj[item]) {
				obj[item] = 0;
			}
			obj[item]++;
			return obj;
		}, {});
		console.log('8.统计给出数组中各个物品的数量');
		console.log(transportation);
	</script>
</html>
